import java.awt.GridBagLayout;
import javax.swing.JPanel;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class EditLinePanel extends JPanel {
	public static final String NEW = "Tao moi ...";  //  @jve:decl-index=0:
	
	private static final long serialVersionUID = 1L;
	private AutoCompletePanel linesPanel = null;
	private JPanel jPanel = null;
	private JLabel jLabel = null;
	private JTextField nameTextField = null;
	private AutoCompletePanel allStation = null;
	private AutoCompletePanel inLineStation = null;
	private JButton addButton = null;
	private JButton removeButton = null;
	private JButton updateButton = null;
	private Item currentLine = null;

	/**
	 * This is the default constructor
	 */
	public EditLinePanel() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	JList linesJList = null;

	private JButton deleteButton = null;
	private void initialize() {
		GridLayout gridLayout = new GridLayout();
		gridLayout.setRows(1);
		gridLayout.setColumns(2);
		this.setLayout(gridLayout);
		this.setSize(300, 200);
		
		JPanel panel = new JPanel(new GridLayout(1, 2));
		panel.add(getLinesPanel());
		panel.add(new JPanel());
		this.add(panel, null);
		this.add(getJPanel(),null);
	}

	/**
	 * This method initializes linesPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private void initLinesPanel() {
		ArrayList<Item> items = new ArrayList<Item>();
		BusLineManager lm = null;
		try {
			lm = BusLineManager.getInstance();
		} catch (SQLException e) {
		}
		ArrayList<BusLine> lines = lm.getAllBusLine();
		items.add(new Item(0, NEW));
		for (BusLine line : lines) {
			Item item = new Item();
			item.id = line.getId();
			item.name = line.getName();
			items.add(item);
		}
		getLinesPanel().setItems(items);
		getLinesPanel().updateItemList();
	}
	
	private AutoCompletePanel getLinesPanel() {
		if (linesPanel == null) {
			linesPanel = new AutoCompletePanel();
			linesPanel.setLabel("Chọn tuyến:");
			initLinesPanel();	
			linesJList = getLinesPanel().getItemList();
			linesJList.addListSelectionListener(new ListSelectionListener() {
				
				@Override
				public void valueChanged(ListSelectionEvent e) {
					Item item = getLinesPanel().getSelectedItem();
					currentLine = item;
					if (item == null) return;
					if (item.name.equals(NEW)) {
						getNameTextField().setText("");
						getInLineStation().setItems(new ArrayList<Item>());
					} else {
						getNameTextField().setText(item.name);
						
						ArrayList<Item> items = new ArrayList<Item>();
						StationManager sm = null;
						try {
							sm = StationManager.getInstance();
						} catch (SQLException e1) {
						}
						ArrayList<Station> stations = sm.getStationInLine(item.id);
						for (Station station : stations) {
							item = new Item();
							item.id = station.getId();
							item.name = station.getName();
							items.add(item);
						}
						getInLineStation().setItems(items);
						getInLineStation().updateItemList();
					}
				}
			});
		}
		return linesPanel;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel = new JLabel();
			jLabel.setText("Tên: ");
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout());
			JPanel tmp = new JPanel(new GridLayout());
			tmp.add(jLabel);
			tmp.add(getNameTextField());
			jPanel.add(tmp, BorderLayout.NORTH);
			//jPanel.add(getNameTextField(), null);
			tmp = new JPanel(new GridLayout());
			tmp.add(getAllStation(), null);
			tmp.add(getInLineStation(), null);
			jPanel.add(tmp, BorderLayout.CENTER);
			
			tmp = new JPanel(new GridLayout());
			JPanel tmp2 = new JPanel(new GridLayout());
			tmp2.add(getAddButton());
			tmp2.add(getRemoveButton());
			
			tmp.add(tmp2);
			tmp.add(getUpdateButton());
			tmp.add(getDeleteButton(), null);
			jPanel.add(tmp, BorderLayout.SOUTH);
		}
		return jPanel;
	}

	/**
	 * This method initializes nameTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNameTextField() {
		if (nameTextField == null) {
			nameTextField = new JTextField();
		}
		return nameTextField;
	}

	/**
	 * This method initializes allStation	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private AutoCompletePanel getAllStation() {
		if (allStation == null) {
			allStation = new AutoCompletePanel();
			allStation.setLabel("Tất cả các bến:");
			
			ArrayList<Item> items = new ArrayList<Item>();
			StationManager sm = null;
			try {
				sm = StationManager.getInstance();
			} catch (SQLException e) {
			}
			ArrayList<Station> stations = sm.getAllStation();
			for (Station station : stations) {
				Item item = new Item();
				item.id = station.getId();
				item.name = station.getName();
				items.add(item);
			}
			//tab Tim duong
			getAllStation().setItems(items);
			getAllStation().updateItemList();
		}
		return allStation;
	}

	/**
	 * This method initializes inLineStation	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private AutoCompletePanel getInLineStation() {
		if (inLineStation == null) {
			inLineStation = new AutoCompletePanel();
			inLineStation.setLabel("Các bến trong tuyến:");
		}
		return inLineStation;
	}

	/**
	 * This method initializes addButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getAddButton() {
		if (addButton == null) {
			addButton = new JButton("+");
			addButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ArrayList<Item> selectedItems = getAllStation().getSelectedItems();
					ArrayList<Item> inLineStations = getInLineStation().getItems();
					
					
					for (Item item : selectedItems) {
						boolean shouldAdd = true;
						for (Item item2 : inLineStations) {
							if (item.id == item2.id) {
								shouldAdd = false;
								break;
							}
						}
						if (shouldAdd) {
							inLineStations.add(item);
						}
					}
					getInLineStation().setItems(inLineStations);
					getInLineStation().getItemList().updateUI();
					EditLinePanel.this.updateUI();
				}
			});
		}
		return addButton;
	}

	/**
	 * This method initializes removeButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getRemoveButton() {
		if (removeButton == null) {
			removeButton = new JButton("-");
			removeButton.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					ArrayList<Item> selectedItems = getInLineStation().getSelectedItems();
					for (Item item : selectedItems) {
						inLineStation.getItems().remove(item);
					}
					getInLineStation().updateItemList();
				}
			});
		}
		return removeButton;
	}

	/**
	 * This method initializes updateButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getUpdateButton() {
		if (updateButton == null) {
			updateButton = new JButton("Cập nhật");
			
			updateButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					Item item = getLinesPanel().getSelectedItem();
					if (getNameTextField().getText().equals("")) {
						JOptionPane.showMessageDialog(null, "Chưa nhập tên");
						return;
					}
						BusLineManager bm = null;
						StationManager sm = null;
						try {
							bm = BusLineManager.getInstance();
							sm = StationManager.getInstance();
						} catch (SQLException e2) {
							return;
						}
						BusLine newbl = new BusLine(getNameTextField().getText());
						ArrayList<Item> items = getInLineStation().getItems();
						ArrayList<Station> newStations = new ArrayList<Station>();
						for (Item i : items) {
							Station station = sm.getStation(i.id);
							if (station != null) {
								newStations.add(station);
							}
						}
						
						boolean result = false;
						if ( currentLine == null || currentLine.name.equals(NEW)) {
							result = bm.addBusLine(newbl, newStations);
						} else {
							result = bm.updateBusLine(currentLine.id, newbl, newStations);
						}
						if (!result) {
							JOptionPane.showMessageDialog(null, "Hãy chọn tên khác.");
						}
						initLinesPanel();
				}
			});
		}
		return updateButton;
	}

	/**
	 * This method initializes deleteButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getDeleteButton() {
		if (deleteButton == null) {
			deleteButton = new JButton("Xoá");
			
			deleteButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					Item item = getLinesPanel().getSelectedItem();
					if (item == null) return;
					BusLineManager sm = null;
					try {
						sm = BusLineManager.getInstance();
					} catch (SQLException e1) {
					}
					sm.removeBusLine(item.id);
					initLinesPanel();
				}
			});
		}
		return deleteButton;
	}

}
